
/************************************************************************/
/*     	File Name:	Table3.do		      								*/
/*     	Date:   	Junio 2025    			            				*/
/*      Author: 	Juan Acevedo-Ossa		                        	*/
/*      Purpose:   Replication file for "The Indirect Effects of	    */
/*				   Structural Power										*/
/*      Input Files:  gvcnousnoch.csv, gvconlyus.csv gvconlych.csv		*/	
/*                    panel_unga.csv                                	*/	
/*      Output File:   "Table 3" and "Table 4"                          */
/*      Total Processing time:  3 hrs 36 minutes                        */
/*      Machine:  16.0 GB RAM - Intel Core i7 @ 2.50 GHz 16 cores       */
/*      Software: Stata 18 SE                                           */
/************************************************************************/

*Open a log file: the values of F-test will can be accessed inside the log file once the estimation is completed 
log using table3.txt, text 
timer clear
timer on 1

****************************************************************************
*ESTIMATION ML (computationally expensive. Stata doesn't allow multiple Dvarlags with spregress and ML)
****************************************************************************

*1. Load ML program

drop _all

pr drop _all

set more off
*pause off
*set rmsg on
*set rmsg off

***********************
*LIKELIHOOD  EVALUATOR*
***********************
                                                        
program define splag_ll

*Instruct program to use "lnf," "mu," "rho1," "rho2," "rho3," and "sigma" are arg's to program
args lnf mu rho1 rho2 rho3 sigma

*Instruct "A," "rSL1," "rSL2," "rSL3," and "rSL4" to be temporary variables
*Code for class example drops 4th ((time-lagged) endogenous) spatial-lag
tempvar A rSL1 rSL2 rSL3 /* rSL4 */

*Define 'rSL1'
gen double `rSL1'=`rho1'*SL1

*Define 'rSL2'
gen double `rSL2'=`rho2'*SL2

*Define 'rSL3'
gen double `rSL3'=`rho3'*SL3

*Define 'rSL4'
*gen double `rSL4'=`rho4'*SL4

*Define p1 (coefficient on spatial lag 1)
scalar p1 = `rho1'

*Define p2 (coefficient on spatial lag 2)
scalar p2 = `rho2'

*Define p3 (coefficient on spatial lag 3)
scalar p3 = `rho3'

*Define p4 (coefficient on spatial lag 4)
*scalar p4 = `rho4'

*Define 1st spatial-lag matrix
matrix p1W1 = p1*W1

*Define 2nd spatial-lag matrix
matrix p2W2 = p2*W2

*Define 3rd spatial-lag matrix
matrix p3W3 = p3*W3

*Define 4th spatial lag matrix
*matrix p4W4 = p4*W4

*Define master (IpW) combined spatial lag matrix
*matrix IpW = I_n - p1W1 - p2W2- p3W3- p4W4

*Define master (IpW) combined spatial lag matrix
matrix IpW = I_n - p1W1 - p2W2- p3W3

*Define 'A' and make into scalar
qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
scalar A = `A'

*Define 'lnf' and end program
*qui replace `lnf'= A + ln(normden($ML_y1-`rSL1'-`rSL2'-`rSL3'-`rSL4'-`mu', 0, `sigma'))
* For Stata9 & before: use "normden" instead of "normalden"

* LOG LIKELHOOD:
*qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'-`rSL3'-`rSL4'-`mu', 0, `sigma'))
qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'-`rSL3'-`mu', 0, `sigma'))
* Note the concentrated likelihood: ln|A| given above, separate from the SSE (& sigma2) part(s) here.
end


**************************************
*LOAD MATRICES IN THE TRADITIONAL WAY*
**************************************
local i 1


cd "`c(current_do)'" 

*All countries but China and the US
import delimited "Data\GVC matrices\gvcnousnoch.csv", varnames(nonames) clear
qui sum v1
global nobs = r(N)
mkmat v1-v$nobs, matrix(W1)
matrix I_n = I($nobs)

*Only the US
drop _all
import delimited "Data\GVC matrices\gvconlyus.csv", clear
mkmat v1-v$nobs, matrix(W2)

*Only China
drop _all
import delimited "Data\GVC matrices\gvconlych.csv", clear
mkmat v1-v$nobs, matrix(W3)


forvalues i=1/6 {

    drop _all
    import delimited "Data\panel_unga.csv", clear

    if `i'== 1 {
        global title_table Model_1
        global Y alignusimp
        global X lalignusimp
    }
    else if `i' == 2 {
        global title_table Model_2
        global Y alignusimp
        global X lalignusimp polyarchy pop_log loggdppercapita corruption_control imf right_wing unsc_non_p developing
    }

    else if `i' == 3 {
        global title_table Model_3
        global Y alignusimp
        global X lalignusimp laidusgdplog sanct_stock ltradegdpuslog polyarchy pop_log loggdppercapita corruption_control imf atop right_wing dip_visits_usa unsc_non_p developing
    }
	
	   if `i'== 4 {
        global title_table Model_4
        global Y alignuschdiff
        global X lalignuschdiff
    }
    else if `i' == 5 {
        global title_table Model_5
        global Y alignuschdiff
        global X lalignuschdiff polyarchy pop_log loggdppercapita corruption_control imf right_wing unsc_non_p developing
    }
    else if `i' == 6 {
        global title_table Model_6
        global Y alignuschdiff
        global X lalignuschdiff sanct_stock ltradegdpuslog laidusgdplog polyarchy pop_log loggdppercapita corruption_control imf atop right_wing dip_visits_usa unsc_non_p developing
    }
   
   

    * Create Y-variable as a matrix (vector)
    mkmat $Y, matrix(Y)

    * Create 1st spatial-lag in matrix and then save as variable also
    matrix SL1 = W1*Y
    svmat SL1, n(SL1)

    * Create 2nd spatial-lag in matrix and then save as variable also
    matrix SL2 = W2*Y
    svmat SL2, n(SL2)

    * Create 3rd spatial-lag in matrix and then save as variable also
    matrix SL3 = W3*Y
    svmat SL3, n(SL3)

    **************************************
    * MODEL 1 *
    **************************************

    * PRODUCE STARTING VALUES VIA OLS
    qui regress $Y $X
    global nobs=e(N)
    matrix OLSb=e(b)
    global OLSsigma=e(rmse)

    * ESTIMATE SPATIAL LAG MODEL
    * Designate model to estimate
    ml model lf splag_ll (mu: $Y=$X) (rho1:) (rho2:) (rho3:) (sigma:) /*(rho4:)*/

    * Specify starting values
    ml init OLSb
    ml init rho1:_cons=0
    ml init rho2:_cons=0
    ml init rho3:_cons=0
    * ml init rho4:_cons=0
    ml init sigma:_cons=$OLSsigma
    * ml max, difficult trace
    ml max, trace
	
	
    estimates store $title_table

    test [rho1]_cons = [rho2]_cons
    test [rho1]_cons = [rho3]_cons
    test [rho2]_cons = [rho3]_cons

    estat ic

}



esttab Model_1 Model_2 Model_3 Model_4 Model_5 Model_6, ///
	mgroups("StateDep" "StateDep" "StateDep" "USChnDiff" "USChnDiff" "USChnDiff", pattern(1 1 1 1 1 1)) /// 
    mtitles("Baseline" "Node" "Full" "Baseline" "Node" "Full") ///
    cells("b(star fmt(4))" se(par fmt(4))) ///
    modelwidth(10) nodepvars ///
    varlabels(lalignusimp "State Dept Y(t-1)" ///
			  lalignuschdiff "Diff USCH Y(t-1)" ///
              polyarchy "Democracy" ///
              pop_log "log(Population)" ///
              loggdppercapita "log(GDP per capita)" ///
              corruption_control "Control of corruption" ///
              imf "IMF commitment" ///
              right_wing "Right Wing" ///
              unsc_non_p "UNSC member" ///
              developing "Developing" ///
			  laidusgdplog "log(USAID/GDP)" ///
              sanct_stock "Sanctions" ///
              ltradegdpuslog "TradeUS/GDPlog (t-1)" ///
              atop "Alliances" ///
              dip_visits_usa "Diplomatic") ///
    order(lalignusimp lalignuschdiff polyarchy pop_log loggdppercapita corruption_control ///
          imf right_wing unsc_non_p developing laidusgdplog sanct_stock ltradegdpuslog ///
           atop dip_visits_usa) ///
    stats(N aic bic ll, fmt(%9.3f) label("Observations" "AIC" "BIC" "LL")) ///
    title("mSTAR models of vote coincidence") ///
    addnotes("* p-value < 0.10; ** p-value < 0.05 level; *** p-value < 0.01 level.")


timer off 1
timer list



****************************************************************************
*                      TABLE 4    F-TESTS                                  -
****************************************************************************

estimates restore Model_3
 test [rho1]_cons = [rho2]_cons
    test [rho1]_cons = [rho3]_cons
    test [rho2]_cons = [rho3]_cons


estimates restore Model_6
 test [rho1]_cons = [rho2]_cons
    test [rho1]_cons = [rho3]_cons
    test [rho2]_cons = [rho3]_cons

log close

log close